package collectionTaks;

import java.util.*;

/** 对List集合排序：排Integer、排String、排Point----练习Lambda表达式使用 */
public class ListSortDemo02 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            list.add(rand.nextInt(100));
        }
        System.out.println(list);//[64, 16, 34, 4, 26, 99, 46, 82, 92, 66]

        Collections.sort(list);
        System.out.println("排序后的list"+list);//升序

        List<String> list1 = new ArrayList<>();
        list1.add("one");
        list1.add("two");
        list1.add("three");
        list1.add("four");
        list1.add("five");
        System.out.println(list1);//[one, two, three, four, five]

        Collections.sort(list1);//按首字母ASCII排序
        System.out.println(list1);//[five, four, one, three, two]

        List<Point> list2 = new ArrayList<>();
        list2.add(new Point(1,2));
        list2.add(new Point(23,3));
        list2.add(new Point(45,64));
        list2.add(new Point(10,4));
        System.out.println(list2);//[(1,2), (23,3), (45,64), (10,4)]

        Collections.sort(list2, new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                return o1.getX()- o2.getX();//按x坐标升序
            }
        });
        System.out.println(list2);//[(1,2), (10,4), (23,3), (45,64)]

        //Lambda表达式
        Collections.sort(list2, (o1,o2)->o1.getY()- o2.getY());//按Y坐标升序
        System.out.println(list2);//[(1,2), (23,3), (10,4), (45,64)]











    }
}
